<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/10 10:14
 * Modified Time: 2019/7/10 10:14
 * Version: 2.0
 */

namespace App\Modules\Inventory\Controllers;


use App\Exceptions\AllValidatorException;
use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\Inventory\Services\GoodsService;
use App\Modules\Inventory\Services\InstockService;
use App\Modules\Pay\Services\PayService;
use Flower\Core\Http\BaseController;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Http\Request;

class InstockController extends BaseController
{
    protected $instockService;
	protected $payService;
    public function __construct(InstockService $instockService,PayService $payService)
    {
        $this->instockService = $instockService;
        $this->payService = $payService;
    }

    public function getRules()
    {
        return [
            'goodsSale' => [
                'store_id' => [
                    'title' => '店铺id',
                    'rule' => 'required',
                    'message' => []
                ],
//                'goods_id' => [
//                    'title' => '商品id',
//                    'rule' => 'required',
//                    'message' => []
//                ],
//                'count' => [
//                    'title' => '销售量',
//                    'rule' => 'required',
//                    'message' => []
//                ],
                'payment' => [
                    'title' => '支付方式',
                    'rule' => 'required',
                    'message' => []
                ],
//                'real_price' => [
//                    'title' => '实际支付金额',
//                    'rule' => 'required',
//                    'message' => []
//                ],
            ],
            'goodsInstockRecord' => [
                'store_id' => [
                    'title' => '店铺id',
                    'rule' => 'required',
                    'message' => []
                ],
            ],
            'goodsUse' => [
                'store_id' => [
                    'title' => '店铺id',
                    'rule' => 'required',
                    'message' => []
                ],
                'goods_data' => [
                    'title' => '商品信息',
                    'rule' => 'required',
                    'message' => []
                ],
            ],
            'getMyselfUseGoodsRecord' => [
                'store_id' => [
                    'title' => '店铺id',
                    'rule' => 'required',
                    'message' => []
                ],
            ],
            'getMyselfGoodsSaleRecord' => [
                'store_id' => [
                    'title' => '店铺id',
                    'rule' => 'required',
                    'message' => []
                ],
            ],
        ];
    }

    /**
     * 商品入库
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     * @throws \App\Exceptions\AllValidatorException
     */
    public function goodsInstock(Request $request)
    {

        $store_id = $request->input('store_id');
        $goods_id = $request->input('goods_id');
        $count = $request->input('count');
        $price = $request->input('price');
        $this->instockService->goodsInstock($store_id,$goods_id,$count,$price);

        return $this->success('插入成功');

    }

	/**
	 * 商品销售
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\NotFoundException
	 */
//    public function goodsSale(Request $request)
//    {
//        $user_id = FLAuth::id();
//        $store_id = $request->input('store_id');
//        $goods_id = $request->input('goods_id');
//        $real_price = $request->input('real_price');
//        $count = $request->input('count');
//        $payment = $request->input('payment');
//        $remark = $request->input('remark');
//        $goodsSaleInfo = $this->instockService->goodsSale($user_id, $store_id, $goods_id,$real_price, $count, $payment, $remark);
//		$goods = $goodsSaleInfo['goods'];
//		unset($goodsSaleInfo['goods']);
//        $this->payService->Pay($goods,3,$request->bill_id,$goodsSaleInfo);
//        return $this->success('操作成功');
//    }


	public function goodsSale(Request $request)
	{
		$user_id = FLAuth::id();
		$store_id = $request->input('store_id');
		$goods = $request->input('goods');
		$payment = $request->input('payment');
		$remark = $request->input('remark');
		$goods = json_decode($goods,true);
		$bill_id = $request->input('bill_id');
		if(empty($goods)){
			throw new AllValidatorException(['msg'=>'goods参数错误']);
		}
		// $goods=[{"goods_id": 1,"count":2,"price":2}];
//		$main_recommend = $request->input('main_recommend');
//		$assist_recommend = $request->input('assist_recommend');
		$result = $this->instockService->goodsSale($bill_id,$user_id, $store_id, $goods,$payment, $remark);
		$order = $result['order'];
		unset($result['order']);
		$this->payService->Pay($order,3,$request->bill_id,$result);
		return $this->success('操作成功');
	}






    /**
     * 获取入库记录
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function goodsInstockRecord(Request $request)
    {
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize', 10);

        $data = $this->instockService->goodsInstockRecord($store_id, $pageSize);

        return $this->display($data, '操作成功');
    }

    /*
     * 商品使用
     */
    public function goodsUse(Request $request)
    {
        $store_id = $request->input('store_id');
        $goods_data = $request->input('goods_data');

        $this->instockService->goodsUse($store_id, $goods_data);

        return $this->success('操作成功');

    }

	/**
	 * 店长获取理发师领料记录
	 * @param Request $request
	 * @return mixed
	 */
    public function getBarberUseGoodsRecord(Request $request)
    {

	    $store_id = $request->input('store_id');
	    $pageSize = $request->input('pageSize', 10);
        $record = $this->instockService->getBarberUseGoodsRecord($store_id,$pageSize);
        return $this->display($record, '查询成功');
    }

    /*
     * 理发师获取本人领料记录
     */
    public function getMyselfUseGoodsRecord(Request $request)
    {

        $store_id = $request->input('store_id');
        $user_id = FLAuth::id();
        $pageSize = $request->input('pageSize', 10);
        $record = $this->instockService->getMyselfUseGoodsRecord($store_id, $user_id, $pageSize);

        return $this->display($record, '查询成功');

    }

    public function getStoreGoodsSaleRecord(Request $request)
    {
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize', 10);
        $record = $this->instockService->getStoreGoodsSaleRecord($store_id,$pageSize);

        return $this->display($record, '查询成功');
    }

    /**
     * 获取本人销售记录
     * @param Request $request
     */
    public function getMyselfGoodsSaleRecord(Request $request)
    {
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize', 10);
        $record = $this->instockService->getMyselfGoodsSaleRecord($store_id,$pageSize);

        return $this->display($record, '查询成功');
    }


}